package in.muton.edushare.grading.panels.prevgrades.old;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.grading.panels.studentGrades.GradingUtil;
import in.muton.edushare.grading.panels.studentGrades.GradingUtil.CourseGradeHelper;

import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.edushare.common.dao.EnrolmentsDAO;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Users;

public class CourseGradeField extends TextField<String> {
	GradingUtil gUtil = null;
	private IModel<Users> userModel;
	private IModel<CourseInstances> courseModel;
	private boolean error = false;
	private String errorString = "";
	private int groupId;
	private IModel<Batches> batchModel;
	private int semNum;

	public CourseGradeField(String id, IModel<Users> user,
			IModel<Batches> batchModel, IModel<CourseInstances> courseInst,
			int semNum, int groupId) {
		super(id);
		this.semNum = semNum;
		this.groupId = groupId;
		this.batchModel = batchModel;
		this.userModel = user;
		this.courseModel = courseInst;
		if (courseModel != null)
			gUtil = new GradingUtil(courseModel);
		setModel(new GradeModel());
	}

	public boolean isError() {
		return error;
	}

	public String getError() {
		return errorString;
	}

	public void setCourseModel(IModel<CourseInstances> courseModel) {
		this.courseModel = courseModel;
		gUtil = new GradingUtil(this.courseModel);
	}

	private void setError(String errorString) {
		error = true;
		this.errorString = errorString;
	}

	private void clearError() {
		error = false;
		this.errorString = "";
	}

	private class GradeModel extends Model<String> {
		@Override
		public void setObject(String grade) {
			cgh.temp = grade;
			EnrolmentsDAO eDAO = new EnrolmentsDAO(EduShareSession.get()
					.getSessionORMInterface());
			eDAO.enrolBatchStudents(courseModel.getObject(), batchModel
					.getObject(), semNum, userModel.getObject(), groupId);
			if (cgh.isLetterGrade) {
				if (!gUtil.isValidKey(cgh.temp)) {
					setError(cgh.temp + " for student "
							+ cgh.user.getObject().getCompleteUserName()
							+ " is not a valid grade.");
					return;
				}
				clearError();
				gUtil.saveMarks(cgh);
			} else {
				try {
					if (!gUtil.isMarksOK(Double.parseDouble(cgh.temp))) {
						errorString = "Marks for students must be between 0 and 100.";
						return;
					}
					clearError();
					gUtil.saveMarks(cgh);
				} catch (NumberFormatException e) {
					setError("Marks for students must be valid numbers between 0 and 100");
				}
			}
		}

		@Override
		public String getObject() {
			if (cgh == null && gUtil != null) {
				cgh = gUtil.getGrades(userModel.getObject());
				return cgh.temp;
			}
			return "";
		}

		private CourseGradeHelper cgh;
		private static final long serialVersionUID = -3697636576576808852L;
	}

	@Override
	public boolean isVisible() {
		return courseModel != null;
	}

	private static final long serialVersionUID = -4086650574654775653L;
}
